<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();

class user extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $data['konten'] = 'users';
            //$this->load->view('home', $data);
            redirect('user/lihatUser');
        } else {
//If no session, redirect to login page
            redirect('login', 'refresh');
        }
//  redirect('user/lihatUser');
    }

    function tambahUser() {
        if ($this->input->post('daftar') != null) {
            $this->load->model('mModel');
            if ($this->input->post('username', true) != null && $this->input->post('password', true) != null) {

                $user['username'] = $this->input->post('username', true);
                $user['value'] = $this->input->post('password', true);
                $user['attribute'] = 'Cleartext-Password';
                $user['op'] = ':=';

                $this->mModel->insertRadius('radcheck', $user);

                if ($this->input->post('profile', true) != null) {
                    $profil['username'] = $user['username'];
                    $profil['value'] = $this->input->post('profile', true);
                    $profil['attribute'] = 'Mikrotik-Group';
                    $profil['op'] = '=';

                    $this->mModel->insertRadius('radreply', $profil);
                }

                if ($this->input->post('masaAktif', null) != null) {
                    date_default_timezone_set('Asia/Jakarta');
                    $tanggalSekarang = date("m d Y G:i:s");

                    $data = $this->input->post('masaAktif', null);
                    $pecah = explode(" ", $data);

                    switch ($pecah[1]) {
                        case 'h' :
                            $tanggalBaru = date("F d Y G:i:s", strtotime('+' . $pecah[0] . ' hours'));
                            break;
                        case 'd' :
                            $tanggalBaru = date("F d Y G:i:s", strtotime('+' . $pecah[0] . ' days'));
                            break;
                        case 'w' :
                            $tanggalBaru = date("F d Y G:i:s", strtotime('+' . $pecah[0] . ' weeks'));
                            break;
                        case 'm' :
                            $tanggalBaru = date("F d Y G:i:s", strtotime('+' . $pecah[0] . ' months'));
                            break;
                    }
                    $aktif['username'] = $user['username'];
                    $aktif['value'] = $tanggalBaru;
                    $aktif['attribute'] = 'Expiration';
                    $aktif['op'] = ':=';

                    $this->mModel->insertRadius('radcheck', $aktif);
                }

                if ($this->input->post('no', true) != null) {
                    $kontak['username'] = $user['username'];
                    $kontak['telpon'] = $this->input->post('no', true);

                    $this->mModel->insertRadius('user', $kontak);
                }
//                $data['msg'] = 'User ' . $user['username'] . ' berhasil ditambahkan';
                $data['alert'] = 'alert alert-success';
//                $this->load->view('users_success', $data);
                redirect('user/lihatUser2', $data);
            } else {
                
            }
        }
    }

    function lihatUser() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $this->load->model('mModel');

            $config['base_url'] = base_url() . "user/lihatUser";
            $config['total_rows'] = $this->mModel->jumlahbaris()->num_rows();
            $config['per_page'] = 10;
            $config['first_link'] = 'Awal';
            $config['last_link'] = 'Terakhir';
            $config['cur_page'] = 1;
            $config['full_tag_open'] = "<div class='pagination'><ul>";
            $config['full_tag_close'] = '</ul></div>';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['cur_tag_open'] = "<li class='active'><span>";
            $config['cur_tag_close'] = "</span></li>";
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $this->pagination->initialize($config);
            $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
            $data['table'] = $this->mModel->user_page($config["per_page"], $page);
            $data['links'] = $this->pagination->create_links();


            $data['isi'] = $this->mModel->getUser();
            $data['telpon'] = $this->mModel->telpon();
            $data['konten'] = 'users';
            $this->load->view('home', $data);
        } else {
//If no session, redirect to login page
            redirect('login', 'refresh');
        }
    }

    function lihatUser2() {
        $this->load->model('mModel');

        $config['base_url'] = base_url() . "user/lihatUser2";
        $config['total_rows'] = $this->mModel->jumlahbaris()->num_rows();
        $config['per_page'] = 10;
        $config['first_link'] = 'Awal';
        $config['last_link'] = 'Terakhir';
        $config['cur_page'] = 1;
        $config['full_tag_open'] = "<div class='pagination'><ul>";
        $config['full_tag_close'] = '</ul></div>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li class='active'><span>";
        $config['cur_tag_close'] = "</span></li>";
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $this->pagination->initialize($config);
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['table'] = $this->mModel->user_page($config["per_page"], $page);
        $data['links'] = $this->pagination->create_links();

        $data['isi'] = $this->mModel->getUser();
        $data['telpon'] = $this->mModel->telpon();
        $this->load->view('users_success', $data);
    }

    function lihatSMS() {
        $this->load->model('mModel');
        $data['isi'] = $this->mModel->getSMS();
        $data['konten'] = 'users';
        $this->load->view('home', $data);
    }

    function edit() {
        $data['konten'] = 'users_edit';
        $this->load->view('home', $data);
    }

    function logout() {
        $this->session->unset_userdata('logged_in');
        session_destroy();
        redirect('home', 'refresh');
    }

    function edit_user($no) {
        $data['judul'] = 'Update Data User';
        $this->load->model('mmodel');
        $data['edit'] = $this->mmodel->edit_user($no);
        $data['konten'] = 'users_edit';
        $this->load->view('home', $data);
    }

    function simpan_edit_user() {
        $no = $this->input->post('no');
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $profile = $this->input->post('profile');
        $telpon = $this->input->post('telpon');

        $data['judul'] = 'Update Data Codeigniter';
        $this->load->model('mmodel');
        $data['edit'] = $this->mmodel->simpan_edit_user($no, $username, $telpon);
        $data['notifikasi'] = 'Data telah berhasil disimpan';
        $this->load->view('users_edit_success', $data);
    }

    function hapusUser($no) {
        $this->load->model("mmodel");

        if ($no > 0) {
            $this->mmodel->hapusUser($no);
        }
        redirect("user/lihatUser");
    }

    function hapusSemua() {
        $this->load->model('mmodel');
        $this->mmodel->hapusSemua();
        $this->mmodel->hapusSemuaUser();
        $this->mmodel->hapusSemuaProfile();
        $data['hapus'] = "Data telah dihapus";
        redirect("user/lihatUser", $data);
    }

    function cari() {
        $this->load->model('mmodel');
        $data['user'] = $this->mmodel->caridata();
        if ($data['user'] == null) {
            redirect('user/cariError');
        } else {
            $data['konten'] = 'users';
            $this->load->view('home', $data);
        }
    }

    function cariError() {
        $data['konten'] = 'cariError';
        $this->load->view('home', $data);
    }

}

?>